#! /usr/bin/python
# -*- coding: utf-8 -*-
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# DataCursor objects inherits from Cursor object to override some of its members
# to draw rulers on the cursor.
#_________________________________________________________
# A.Z. - 10/07 => Creation
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
import numpy
from pylab import *
import qviewF
from globalVariables import gv
from matplotlib.widgets import Cursor



class DataCursor(Cursor):
  __f    = None  # 1, 2 or 3 acording to the three figures
  __a    = None
  __xstr = None
  __ystr = None


   
  def __init__(self, f, a, ax, useblit=True, **lineprops):
    Cursor.__init__(self, ax, useblit=True, **lineprops)
    self.__f    = f
    self.__a    = a
    self.__xstr = ''
    self.__ystr = ''
    if 1>self.__f>3: qviewF.Abort("Wrong figure number: %d"%self.__f)



  def onmove(self, event):
    """
    we override event.xdata to force it to snap-to nearest data
    item here we assume t is sorted and I'll use searchsorted
    since it is a little faster, but you can plug in your nearest
    neighbor routine, eg to grab the closest x,y point to the
    cursor
    """
    

    # Depending on the type of notification (mouse movement or key press),
    # changes the id of the point over which the cursor is now

    if event.name=="motion_notify_event":  # Mouse movement
      # Over which figure and axes the mouse is?
      for i in range(0,gv.ny):
        subplot = None
        if self.__f == 1: subplot = gv.SO[i]
        if self.__f == 2: subplot = gv.SE[i]
        if self.__f == 3: subplot = gv.SD[i]
        if event.inaxes == subplot:
          gv.axes   = i
          gv.figure = self.__f
      # Default values if couldn't identify where the mouse is
      if 0>gv.axes    or gv.axes>=gv.ny: gv.axes   = 0
      if 0>=gv.figure or gv.figure>3   : gv.figure = 2


      # Identify the point id
      xdata = event.xdata
      if self.__f==1 or self.__f==2:
        gv.id = numpy.searchsorted(gv.t, xdata)
        gv.id = min(gv.nt-1, gv.id)
      else:
        gv.id = numpy.searchsorted(gv.f, xdata)
        gv.id = min(gv.nf-1, gv.id)


    elif event.name=="key_press_event":    # Key press

      # If the new id is out of the allowed range, put it on the range again
      if gv.axes==self.__a:
        size = gv.nt                   # Default, over raw or reduced figures
        if gv.figure==3: size= gv.nf   # If over the DFT figure
        while gv.id<0    :  gv.id += size
        while gv.id>=size:  gv.id -= size

    # Draw the cursor
    if   gv.figure==1:
      event.xdata = gv.t[gv.id]
      event.ydata = gv.y[gv.axes][gv.id]
    elif gv.figure==2:
      event.xdata = gv.t[gv.id]
      event.ydata = gv.Cy[gv.axes][gv.id]
    elif gv.figure==3:
      event.xdata = gv.f[gv.id]
      event.ydata = gv.dft[gv.axes][gv.id]
    self.__xstr = '%1.3f'%event.xdata
    self.__ystr = '%1.3f, n=%d' % (event.ydata,gv.id+1)
    Cursor.onmove(self, event)
    self.canvas.toolbar.mouse_move(event)


  def fmtx(self, x):
    return self.__xstr

  def fmty(self, y):
    return self.__ystr
